﻿using Simple.Framework.Ioc;
using SimpleNet.Handle;
using SimpleNet.Net;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/*********************************************************
 * 命名空间 SimpleNet.example
 * 接口名称 MyMessageHandle
 * 开发人员：ADMIN
 * 创建时间：2023/12/18 10:31:22
 * 描述说明：
 * 更改历史：
 * 
 * *******************************************************/
namespace SimpleNet.example
{
	internal class MyMessageHandle : MessageHandle
	{
		public MyMessageHandle(IContainer container) : base(container)
		{
		}


		public override void AnalysisHandle(BlockingCollection<IDataPack> qdataPacks, byte[] data, int length)
		{
			IDataPack dataPack= this.container.GetService<IDataPack>();
			dataPack.ReceiveBuffer = new byte[length];
			Array.Copy(data,0,dataPack.ReceiveBuffer,0,length);
			SetDataPack(dataPack);
			qdataPacks.Add(dataPack);
		}

		public override void Handle(IContext context)
		{
			base.Handle(context);
		}

		public override void Handled(IContext context)
		{
		}

		public override void SetDataPack(IDataPack dataPack)
		{
			dataPack.HeaderBuffer=new byte[2] ;
			Array.Copy(dataPack.ReceiveBuffer,0,dataPack.HeaderBuffer,0,2);

			dataPack.CommandID = dataPack.ReceiveBuffer[2];

			dataPack.TailBuffer = new byte[2];
			Array.Copy(dataPack.ReceiveBuffer, dataPack.ReceiveBuffer.Length-2, dataPack.TailBuffer,0,2);

			dataPack.Length = dataPack.ReceiveBuffer.Length-dataPack.TailBuffer.Length -1-dataPack.HeaderBuffer.Length;
			dataPack.Complete = true;
		}

	}
}
